﻿(function ($) {
    $.fn.listpage = function (option) {
        option = $.extend({
            showselected: false,
            querytableSlt: ".querytable",
            submitSlt: ".submit",
            dataRefreshSlt: ".dataRefresh",
            pagesizeSlt: ".pagesize",
            pageindexSlt: ".pageindex",
            pagerbuttonSlt: ".pagerbutton",
            pkSlt: "[name='pk']",
            gotoSlt: ".goto"
        }, option);
        return this.each(function () {
            var view = $(this);
            var settings = $.extend({}, option);
            var helper = new $.listpageHelper(view, settings);
            view.helper("listpage", helper);
            helper.init();
        });
    };


    $.listpageHelper = function (jo, setting) {
        this.jo = jo;
        this.setting = setting;
        this.items = null;
        var _this = this;
        this.init = function () {
            this.jo.find(this.setting.submitSlt).click(function () {
                _this.query();
            });
            this.rebind();
        };
        this.rebind = function () {
            var panelid = _this.jo.closest("[panelid]").attr("panelid");
            _this.jo.find(_this.setting.pagesizeSlt).change(function () {
                var pagesize = $(this).val();
                _this.sizeTo(pagesize);
            });
            _this.jo.find(_this.setting.pagerbuttonSlt + " [pg]").click(function () {
                var page = $(this).attr("pg");
                _this.pageTo(page);
                return false;
            });
            _this.jo.find(_this.setting.gotoSlt).click(function () {
                var page = _this.jo.find(_this.setting.pageindexSlt).val();
                _this.pageTo(page);
                return false;
            });
        }
        this.getData = function () {
            var obj = {};
            if (_this.jo.has(_this.setting.pageindexSlt)) {
                obj.page = _this.jo.find(_this.setting.pageindexSlt).val();
            }
            if (_this.jo.has(_this.setting.pagesizeSlt)) {
                obj.pagesize = _this.jo.find(_this.setting.pagesizeSlt).val();
            }
            if (_this.jo.has(_this.setting.pkSlt)) {
                obj.id = [];
                _this.jo.find(_this.setting.pkSlt).each(function () {
                    obj.id.push($(this).val());
                });
                if (obj.id.length == 0) {
                    obj.id = null;
                }
            }
            var queryobj = _this.jo.find("form").serializeArray();
            if (queryobj) {
                for (var i = 0; i < queryobj.length; i++) {
                    obj[queryobj[i].name] = queryobj[i].value;
                }
            }
            return obj;
        };
        this.query = function () {
            var data = _this.getData();
            var option = {
                url: _this.jo.find("form").attr("action"),
                tabid: _this.jo.closest("[panelid]").attr("panelid"),
                fill: _this.setting.dataRefreshSlt,
                type: "html",
                backtype: "fill",
                data: data,
                successmsg: null,
                aftersuccess: function () {
                    _this.rebind();
                    _this.jo.layout();
                }
            };
            App.getAjaxManager().ajaxpost(option);
        };
        this.pageTo = function (page) {
            var data = _this.getData();
            data.page = page;
            var option = {
                url: _this.jo.find("form").attr("action"),
                tabid: _this.jo.closest("[panelid]").attr("panelid"),
                fill: _this.setting.dataRefreshSlt,
                type: "html",
                backtype: "fill",
                data: data,
                successmsg: null,
                aftersuccess: function () { _this.rebind(); _this.jo.layout(); }
            };
            App.getAjaxManager().ajaxpost(option);
        };
        this.sizeTo = function (pagesize) {
            var data = _this.getData();
            data.page = 1;
            data.pagesize = pagesize;
            var option = {
                url: _this.jo.find("form").attr("action"),
                tabid: _this.jo.closest("[panelid]").attr("panelid"),
                fill: _this.setting.dataRefreshSlt,
                type: "html",
                backtype: "fill",
                data: data,
                successmsg: null,
                aftersuccess: function () { _this.rebind(); _this.jo.layout(); }
            };
            App.getAjaxManager().ajaxpost(option);
        }
    }
})(jQuery);